function [t1 x1 y1 a1 xo yo ao saccade1 sac_thres1 blink1 speed1 vel_x1 vel_y1 shrink1]=EL1k_blinkremover_sacdetect_trial(t, x, y, a, blink_Xstd, sacc_Xstd, plot_it)
	%[x1 y1 a1 saccade1 sac_thres1 blink1 speed1 vel_x1 vel_y1 shrink1]=EL1k_blinkremover_sacdetect_trial(t, x, y, a, blink_Xstd, sacc_Xstd, plot_it)
	% x & y data must be corrected to degrees per pixel first.
	% inputs:
	% t = time in ms
	% x = horizontal position in degrees of visual angle
	% y = vertical position in degrees of visual angle
	% a = area : size of pupil
	% blink_Xstd = STD multiplyer for cutting out 'blink' data and replacing it with interpolated lines
	% sacc_Xstd = STD multiplyer for detecting saccades
	% plot_it = do you want to see my superfluous ploting that takes eons?
	%
	% ~bcoe:  coe@queensu.ca
	
	%
	if nargin==0
		help(mfilename);
		return
	end
	if nargin<4
		warning ([mfilename ' requires 4 input vectors (t, x, y, a) of equal length']) %#ok<WNTAG>
		help(mfilename);
		return
	end
	if nargin<5
		blink_Xstd=2;
		sacc_Xstd=2;
		plot_it=0;
	end
	if nargin<6
		sacc_Xstd=2;
		plot_it=0;
	end
	if nargin<7
		plot_it=0;
	end
	
	
	%EL1k data forces recording to stop between trials so let's do this trial by trial (which sucks, sometimes)
	%temp=unique(diff(t)); should return something like [2 200+ 200+ 200+ ...]; this is the inter trial dead space.
	end_loc=[find(diff(t)>50); length(t)];
	starloc=[1;find(diff(t)>50)+1];
	if length(end_loc)~=length(end_loc)
		error(['length(end_loc)~=length(end_loc) in ' mfilename]);
	end
	%run for entire block;
	[speed vel_x vel_y shrink]=xyt2speed(x,y,t,a);
	speed=filtfilt([1 1 1]/3,1,speed);
	shrink=filtfilt([1 1 1 1]/4,1,shrink);
	%prealocate
	t1=cell(size(starloc));
	x1=t1;
	y1=t1;
	a1=t1;
	xo=t1;
	yo=t1;
	ao=t1;
	saccade1=t1;
	sac_thres1=ones(size(starloc))*-9999;
	blink1=t1;
	speed1=t1;
	vel_x1=t1;
	vel_y1=t1;
	shrink1=t1;
	
	if plot_it;
		fprintf('\nTrial %3d of %3d (%5.1f%%)\n',0,length(starloc),0)
	end
	for tr= 1:length(starloc)
		tt=t(starloc(tr):end_loc(tr));
		tx=x(starloc(tr):end_loc(tr));
		ty=y(starloc(tr):end_loc(tr));% ty(1)=ty(2); %hack
		ta=a(starloc(tr):end_loc(tr));
		xo{tr}=tx;
		yo{tr}=ty;
		ao{tr}=ta;
		
		tblink=ta<(mean((ta(ta>0)))/2); % less than half it's non-zero mean (here's where trial-by-trial is good)
		tspeed=speed(starloc(tr):end_loc(tr));
		tvel_x=vel_x(starloc(tr):end_loc(tr));
		tvel_y=vel_y(starloc(tr):end_loc(tr));
		tshrink=shrink(starloc(tr):end_loc(tr));
		if sum(isnan(tx))==length(tx) || sum(isnan(ty))==length(ty) % bad data
			fprintf(['no eye data\n' repmat(' ',1,26)])
			%x1{tr}=tx; % set up data here and assign to output cells below
			%y1{tr}=ty;
			%a1{tr}=ta;
			sacloc=[];
			sac_thres=-9999;
			tblink=true(size(ta));
			tspeed=ta*0;
			tvel_x=ta*0;
			tvel_y=ta*0;
			tshrink=ta*0;
			%loc=[];
		else % good data
			
			% 			[tspeed tvel_x tvel_y tshrink]=xyt2speed(tx,ty,tt,ta);
			% 			tspeed(tblink)=0;
			% 			tspeed=filtfilt([1 1 1]/3,1,tspeed);
			% 			tshrink=filtfilt([1 1 1 1]/4,1,tshrink);
			
			%if isnan(sum(tx))
			tshrinkstd=std(tshrink(tshrink<10))*blink_Xstd*2;% temp var just to shore up the trial ends
			tx(001)=tx(find(tshrink<tshrinkstd  &~tblink & ~isnan(tx) ,1,'first'));
			tx(end)=tx(find(tshrink<tshrinkstd  &~tblink & ~isnan(tx) ,1,'last' ));
			ty(001)=ty(find(tshrink<tshrinkstd  &~tblink & ~isnan(ty) ,1,'first'));
			ty(end)=ty(find(tshrink<tshrinkstd  &~tblink & ~isnan(ty) ,1,'last' ));
			ta(001)=ta(find(tshrink<tshrinkstd  &~tblink,1,'first'));
			ta(end)=ta(find(tshrink<tshrinkstd  &~tblink,1,'last' ));
			%end
			
			
			if plot_it
				fprintf([repmat('\b',1,26) 'Trial %3d of %3d (%5.1f%%)\n'],tr,length(starloc), tr/length(starloc)*100)
				ttt=tt-tt(1); %just for plotting
				figure(1);clf; hold on;
				ylim([-20 40])
				xlim([0 ttt(end)])
				plot(ttt,tx,'b')
				plot(ttt,ty,'r')
				hline(10,'k')
				plot(ttt,ta/50+10,'k')
				plot(ttt,tspeed/50+10,'g');
				hline(100/50+10,'--g'); % 50 dps
				plot(ttt,abs(tshrink)+10,'y');
				hline((mean(mean(ta(ta>0)))/2)/50+10,'--k'); % dynamic thres
				bliloc=[find(diff([0;tblink;0])>0) find(diff([0;tblink;0])<0)];
				vfill(bliloc(:,1),bliloc(:,2),'m',.5)
			end
			
			s_blink01=filtfilt([1 1 1 1 1 1]/6,1,tblink);
			s_blink02=filtfilt([1 1 1 1 1 1]/6,1,round(s_blink01+.25));
			tblink=logical(round(s_blink02+.25));
			tblink(end)=0;
			loc=find(diff([0; tblink]));
			
			if ~isempty(loc) %actually blinked
				tshrinkstd=std(tshrink(tshrink<10))*blink_Xstd;
				tspeedstd=nanstd(tspeed(tspeed<200))*blink_Xstd;
				if plot_it
					plot(ttt(tblink),ta(tblink)/50+10,'xk');
					vfill(ttt(loc(1:2:end)),ttt(loc(2:2:end)),[],.2 )
					vline(ttt(loc))
					hline(tshrinkstd-30);
					hline(tspeedstd/50-30,'-.g');
				end
				
				sloc=[1; loc(1:2:end);length(tblink)];
				eloc=[1; loc(2:2:end);length(tblink)];
				for i = 2:length(sloc)-1
					%forward
					eee=find(tspeed(eloc(i):sloc(i+1))<tspeedstd &...
						abs(tshrink(eloc(i):sloc(i+1)))<tshrinkstd ...
						, 1)+eloc(i)-1 ;
					if isempty(eee)% try again with higher threshold
						eee=find(tspeed(eloc(i):sloc(i+1))<tspeedstd*2 &...
							abs(tshrink(eloc(i):sloc(i+1)))<tshrinkstd*2 ...
							, 1)+eloc(i)-1 ;
					end
					tblink(eloc(i):eee)=1;
					if plot_it && eee
						xlim([ttt(eloc(i))-50 ttt(eloc(i))+150 ])
						vfill(ttt(eloc(i)), ttt(eee),[],.5)
						%drawnow
					end
					% backward
					sss=find(tspeed(eloc(i-1):sloc(i))<tspeedstd & ...
						abs(tshrink(eloc(i-1):sloc(i)))<tshrinkstd ...
						,1,'last')+eloc(i-1);
					if isempty(sss)% try again with higher threshold
						sss=find(tspeed(eloc(i-1):sloc(i))<tspeedstd*2 & ...
							abs(tshrink(eloc(i-1):sloc(i)))<tshrinkstd*2 ...
							,1,'last')+eloc(i-1);
						if isempty(sss)% try with next saccade
							sss=sloc(i);
							%sloc(i+1)=sloc(i-1);
						end
					end
					tblink(sss:sloc(i))=1;
					if plot_it
						xlim([ttt(sloc(i))-150 ttt(sloc(i))+50 ])
						vfill(ttt(sss), ttt(sloc(i)),[],.5)
						%drawnow
					end
					
					% interpolate
					%try
					tx(sss:eee)=linspace(tx(sss),tx(eee),eee-sss+1);
					ty(sss:eee)=linspace(ty(sss),ty(eee),eee-sss+1);
					ta(sss:eee)=linspace(ta(sss),ta(eee),eee-sss+1);
					% recalculate
					[tspeed tvel_x tvel_y tshrink]=xyt2speed(tx,ty,tt,ta);
					tspeed=filtfilt([1 1 1]/3,1,tspeed);
					tshrink=filtfilt([1 1 1 1]/4,1,tshrink);
					if plot_it
						% replot
						xlim([ttt(sss)-150 ttt(eee)+150 ] )
						plot(ttt(sss:eee), tx(sss:eee),'+b')
						plot(ttt(sss:eee), ty(sss:eee),'+r')
						plot(ttt(sss:eee), ta(sss:eee)/50+10,'+k')
						plot(ttt(sss:eee), tspeed(sss:eee)/50+10,'+g')
						plot(ttt(sss:eee), abs(tshrink(sss:eee))+10,'oy')
					end
					%catch %#ok<CTCH>
					%	drawnow;
					%end
				end % sloc
			end % actually blinked
			% sacdetect
			[sacloc sac_thres] = EL1k_sacdetect(tspeed, sacc_Xstd, mean(diff(tt)));
			%tsac=false(size(tspeed));
			%for ll=1:size(sacloc,1)
			%	tsac(sacloc(ll,1):sacloc(ll,2))=true;
			%end
			if plot_it
				figure(2);hold on;
				ylim([-20 40])
				xlim([0 ttt(end)])
				plot(tt,tx,'.-b')
				plot(tt,ty,'.-r')
				plot(tt(tblink),tx(tblink),'.c')
				plot(tt(tblink),ty(tblink),'.m')
				plot(tt,abs(tshrink)+10,'y');
				hline(10,'k')
				plot(tt,ta/50+10,'k')
				plot(tt,tspeed/50+10,'.-g');
				hline(sac_thres/50+10,'--g'); % sac_thres
				%hline((mean(mean(ta(ta>0)))/2)/50+10,'--k'); % dynamic thres
				axis tight
				if ~isempty(sacloc)
					vfill(tt(sacloc(:,1)),tt(sacloc(:,2)),'g',.5);
				end
				vline(t(end_loc(tr)),'k');
				vline(t(starloc(tr)),'m')
				drawnow
				pause (.1)				
			end % plot_it
		end % bad or good data
		%assign data to output variable here
		
		%x(starloc(tr):end_loc(tr))=tx;
		%y(starloc(tr):end_loc(tr))=ty;
		%a(starloc(tr):end_loc(tr))=ta;
		%vel_x(starloc(tr):end_loc(tr))=tvel_x;
		%vel_y(starloc(tr):end_loc(tr))=tvel_y;
		%speed(starloc(tr):end_loc(tr))=tspeed;
		%saccade(starloc(tr):end_loc(tr))=tsac;
		%blink(starloc(tr):end_loc(tr))=tblink;
		%shrink(starloc(tr):end_loc(tr))=tshrink;
		t1{tr}=tt;
		x1{tr}=tx;
		y1{tr}=ty;
		a1{tr}=ta;
		saccade1{tr}=sacloc;
		sac_thres1(tr)=sac_thres;
		blink1{tr}=tblink;
		speed1{tr}=tspeed;
		vel_x1{tr}=tvel_x;
		vel_y1{tr}=tvel_y;
		shrink1{tr}=tshrink;
	end
	%x1=x;
	%y1=y;
	%a1=a;
end



